// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import echarts from 'echarts'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueCookies from 'vue-cookies'
import { InfiniteScroll } from 'element-ui';
Vue.use(InfiniteScroll)
Vue.use(ElementUI)
Vue.use(VueCookies)

Vue.config.productionTip = false
Vue.prototype.bus = new Vue()
Vue.prototype.$echarts = echarts
Vue.prototype.$axios = axios;


//axios配置请求拦截器,,这样才能将token传给后端，判断登录
axios.interceptors.request.use(config=>{
  if(localStorage.getItem('logintoken')){
      config.headers.Authorization = localStorage.getItem('logintoken')
  }
  return config;
},
error=>{
  return Promise.reject(error)
})
// 过滤器
import * as custom from './utils/util'

Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key])
})
// 路由拦截器
router.beforeEach((to, from, next) => {
//   console.log(to);
  
   
    
      if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
       
          if (Boolean(localStorage.getItem("userInfo"))) { // 通过vuex state获取当前的user是否存在
          
              next();
          } else {
              next({
                  path: '/',
                  query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
              })
          }
      } else {
      
          if (Boolean(localStorage.getItem("userInfo"))) { // 判断是否登录
           
              if (to.path != "/" && to.path != "/login") { //判断是否要跳到登录界面

                  next();
              } else {
               
                  /**
                   * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页 
                   */
                  next({
                      path: '/index'
                  })
              }
          } else {
           
              next();
          }
      }
 
})
new Vue({
  el: '#app',
  router,
  render: h => h(App),
  components: { App },
  template: '<App/>',
 
})

